{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "got html text success\n",
      "got html text success\n",
      "got html text success\n",
      "got html text success\n",
      "got html text success\n",
      "got html text success\n",
      "got html text success\n",
      "got html text success\n",
      "got html text success\n",
      "got html text success\n",
      "got html text success\n",
      "got html text success\n",
      "got html text success\n",
      "got html text success\n",
      "got html text success\n",
      "got html text success\n",
      "got html text success\n",
      "got html text success\n",
      "got html text success\n",
      "got html text success\n"
     ]
    }
   ],
   "source": [
    "#Semaphore 是用于控制进入数量的锁\n",
    "#文件， 读、写， 写一般只是用于一个线程写，读可以允许有多个\n",
    "\n",
    "#做爬虫\n",
    "import threading\n",
    "import time\n",
    "\n",
    "class HtmlSpider(threading.Thread):\n",
    "    def __init__(self, url, sem):\n",
    "        super().__init__()\n",
    "        self.url = url\n",
    "        self.sem = sem\n",
    "\n",
    "    def run(self):\n",
    "        time.sleep(2)\n",
    "        print(\"got html text success\")\n",
    "        self.sem.release()\n",
    "\n",
    "class UrlProducer(threading.Thread):\n",
    "    def __init__(self, sem):\n",
    "        super().__init__()\n",
    "        self.sem = sem\n",
    "\n",
    "    def run(self):\n",
    "        for i in range(20):\n",
    "            self.sem.acquire()\n",
    "            html_thread = HtmlSpider(\"https://baidu.com/{}\".format(i), self.sem)\n",
    "            html_thread.start()\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    sem = threading.Semaphore(3)\n",
    "    url_producer = UrlProducer(sem)\n",
    "    url_producer.start()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
